package com.jt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jt.pojo.Cart;
import com.jt.pojo.Order;
import com.jt.service.DubboCartService;
import com.jt.service.DubboOrderService;
import com.jt.util.UserThreadLocal;
import com.jt.vo.SysResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Reference(check = false)
    private DubboCartService cartService;
    @Reference(check = false)
    private DubboOrderService orderService;

    /**
     * 1.实现订单确认页面跳转
     * url地址: http://www.jt.com/order/create.html
     * 参数说明:  获取userId
     * 返回值结果: 订单确认页面  order-cart.jsp
     * 页面取值方式:  ${carts}
     */
    @RequestMapping("/create")
    public String orderCart(Model model){
        long userId = UserThreadLocal.getUser().getId();
        List<Cart> cartList = cartService.findCartList(userId);
        model.addAttribute("carts", cartList);
        return "order-cart";
    }

    /**
     * 业务需求: 实现订单入库操作
     * url地址: http://www.jt.com/order/submit
     * 参数:    整个表单的序列化    Order对象接收
     * 返回值:  SysResult对象(orderId)
     */
    @RequestMapping("/submit")
    @ResponseBody
    public SysResult saveOrder(Order order){
        //动态获取userId
        long userId = UserThreadLocal.getUser().getId();
        order.setUserId(userId);

        String orderId = orderService.saveOrder(order);
        if(StringUtils.hasLength(orderId))
            return SysResult.success(orderId);

        return SysResult.fail();
    }


    /**
     * 实现订单页面查询
     * url地址: http://www.jt.com/order/success.html?id=71608194173729
     * 参数: id=xxx
     * 返回值结果: 要求转跳到成功页面
     *
     * */
    @RequestMapping("/success")
    public String success(@RequestParam("id") String orderId, Model model){
        Order order = orderService.findOrderById(orderId);
        model.addAttribute("order", order);
        return "success";
    }







    /*
    <input    type="text" name="dog.age"     value="15"/>
    <input    type="text" name="hobbys[0]"     value="敲代码"/>
    * */
   /* class Dog{
        private String name;
        private Integer age;
    }

    class User{
        private String name;
        private Integer age;
        private Dog dog;
        private List<String> hobbys;
    }

    public void xxx(User user){

    }*/



}
